package com.visionet.school.app.web.web.tclass;

import com.alibaba.fastjson.JSONObject;
import com.visionet.school.app.web.common.utils.PageInfo;
import com.visionet.school.app.web.common.web.WebContextFactory;
import com.visionet.school.app.web.entity.enums.MemberType;
import com.visionet.school.app.web.entity.member.vo.MemberClassVo;
import com.visionet.school.app.web.entity.tclass.vo.SchoolClassVo;
import com.visionet.school.app.web.entity.vo.LoginAccountVo;
import com.visionet.school.app.web.service.tclass.SchoolClassService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hxpeng on 2017/3/1.
 */

@Controller
@RequestMapping("/class")
public class SchoolClassController {


    @Autowired
    private SchoolClassService schoolClassService;


    @RequestMapping(value = "/findClassByName", method = RequestMethod.POST)
    public ResponseEntity<?> findClassByName(@RequestBody JSONObject jsonObject){
        if(jsonObject == null || StringUtils.isBlank(jsonObject.getString("className"))){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("class",schoolClassService.findOneByName(jsonObject.getString("className")));
        return new ResponseEntity(map , HttpStatus.OK);
    }


    @RequestMapping(value = "/findClassByMemberId", method = RequestMethod.GET)
    public ResponseEntity<?> findClassByMemberId(String memberId){
        if(StringUtils.isBlank(memberId)){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("schoolClass",schoolClassService.findClassByMemberId(memberId));
        return new ResponseEntity(map , HttpStatus.OK);
    }

    @RequestMapping(value = "/findClassInfo", method = RequestMethod.GET)
    public ResponseEntity<?> findClassInfo(String id){
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isBlank(id)){
            return new ResponseEntity(map , HttpStatus.NOT_ACCEPTABLE);
        }
        map.put("schoolClass",schoolClassService.findOneById(id));
        return new ResponseEntity(map , HttpStatus.OK);
    }


    @RequestMapping(value = "/getClassList", method = RequestMethod.POST)
    public ResponseEntity<?> getList(@RequestBody SchoolClassVo schoolClassVo){
        if(schoolClassVo == null){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        if(schoolClassVo.getPageInfo() == null){
            schoolClassVo.setPageInfo(new PageInfo());
        }
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("pageResult",schoolClassService.getClassPage(schoolClassVo,"createDate"));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    @RequestMapping(value = "/getClassTeacher", method = RequestMethod.GET)
    public ResponseEntity<?> getClassTeacher(String id){
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isBlank(id)){
            return new ResponseEntity(map , HttpStatus.NOT_ACCEPTABLE);
        }
        try {
            map.put("schoolClass", schoolClassService.getClassMember(id, MemberType.TEACHER.toKey()));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    @RequestMapping(value = "/updateClassDetails", method = RequestMethod.POST)
    public ResponseEntity<?> updateClassDetails(@RequestBody SchoolClassVo schoolClassVo){
        if(schoolClassVo == null){
            return new ResponseEntity(null , HttpStatus.NOT_ACCEPTABLE);
        }
        try{
            schoolClassService.updateClassDetails(schoolClassVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity(null , HttpStatus.OK);
    }


    /**
     * 查询所有学生，根据班级ID
     * @param memberClassVo
     * @return
     */
    @RequestMapping(value = "/getStudentList",method = RequestMethod.POST)
    public ResponseEntity<?> getStudentList(@RequestBody MemberClassVo memberClassVo){
        LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
        if(memberClassVo == null || memberClassVo.getPageInfo() == null || !loginAccountVo.getMemberType().equals(MemberType.TEACHER.toKey())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        memberClassVo.setClassId(loginAccountVo.getClassId());
        memberClassVo.setMemberType(MemberType.STUDENT.toKey());
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("pageResult",schoolClassService.getStudentList(memberClassVo));
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity<Object>(map,HttpStatus.OK);
    }

}
